package com.wyth.lottery.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wyth.lottery.R;
import com.wyth.lottery.activity.ShoppingActivity;
import com.wyth.lottery.base.BaseFragment;
import com.wyth.lottery.bean.hezhi.HezhiBean;
import com.wyth.lottery.bean.hezhi.HezhiBeanNew;
import com.wyth.lottery.bean.hezhi.HezhitypeBean;
import com.wyth.lottery.utils.MathQT;
import com.wyth.lottery.utils.MyApplication;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/2 0002.
 */

public class HeZhiFragment extends BaseFragment {

    @BindView(R.id.rv_hezhi)
    RecyclerView rvHezhi;
    @BindView(R.id.tv_sum)
    TextView tvSum;
    @BindView(R.id.tv_big)
    TextView tvBig;
    @BindView(R.id.tv_small)
    TextView tvSmall;
    @BindView(R.id.tv_single)
    TextView tvSingle;
    @BindView(R.id.tv_double)
    TextView tvDouble;
    @BindView(R.id.ll_times)
    LinearLayout llTimes;
    @BindView(R.id.tv_choose_one)
    TextView tvChooseOne;
    @BindView(R.id.tv_choose_five)
    TextView tvChooseFive;
    @BindView(R.id.tv_choose_ten)
    TextView tvChooseTen;
    @BindView(R.id.tv_notes_sum)
    TextView tvNotesSum;
    @BindView(R.id.tv_money_sum)
    TextView tvMoneySum;
    @BindView(R.id.tv_lottery_time)
    TextView tvLotteryTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_next)
    TextView tvNext;
    @BindView(R.id.tv_return)
    TextView tvReturn;
    private boolean tvBigEnable = true;
    private boolean tvSmallEnable = true;
    private boolean tvSingleEnable = true;
    private boolean tvDoubleEnable = true;
    private List list = new ArrayList();
    private int redcount = 0;
    private int sumHezhi;
    int sumHezhiJine;
    private ArrayList<HezhiBeanNew> hezhi = new ArrayList<>();
    private HezhiAdapter redAdapter;
    @Override
    protected View getSuccessView() {
        View view = LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.fragment_qthree_hezhi, null);
        return view;
    }

    @Override
    protected void initDate() {
        stateLayout.showSuccessView();
        initQThreeView();
    }
    private void initQThreeView() {
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(7, StaggeredGridLayoutManager.VERTICAL);
        rvHezhi.setLayoutManager(staggeredGridLayoutManager);
        ArrayList<HezhiBeanNew> hezhiList;
        hezhiList = MathQT.hezhi();
        hezhi= hezhiList;
        redAdapter = new HezhiAdapter(hezhiList);
        rvHezhi.setAdapter(redAdapter);
    }


    @OnClick({R.id.tv_big, R.id.tv_small, R.id.tv_single, R.id.tv_double, R.id.ll_times, R.id.tv_choose_one, R.id.tv_choose_five, R.id.tv_choose_ten, R.id.tv_next, R.id.tv_return})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_big:
                if (tvBigEnable){
                    tvBig.setBackgroundResource(R.drawable.text_red_10_background);
                    tvBig.setTextColor(getResources().getColor(R.color.white));
                    tvBigEnable = false;
                    initViewEnable(MathQT.hezhiBig());
                }else {
                    tvBig.setBackgroundResource(R.drawable.text_pink_10_background);
                    tvBig.setTextColor(getResources().getColor(R.color.colorTextSelect));
                    tvBigEnable = true;
                    initViewNoEnable(MathQT.hezhiBig());
                }
                break;
            case R.id.tv_small:
                if (tvSmallEnable){
                    tvSmall.setBackgroundResource(R.drawable.text_red_10_background);
                    tvSmall.setTextColor(getResources().getColor(R.color.white));
                    tvSmallEnable = false;
                    initViewEnable(MathQT.hezhiSmall());
                }else {
                    tvSmall.setBackgroundResource(R.drawable.text_pink_10_background);
                    tvSmall.setTextColor(getResources().getColor(R.color.colorTextSelect));
                    tvSmallEnable = true;
                    initViewNoEnable(MathQT.hezhiSmall());
                }
                break;
            case R.id.tv_single:
                if (tvSingleEnable){
                    tvSingle.setBackgroundResource(R.drawable.text_red_10_background);
                    tvSingle.setTextColor(getResources().getColor(R.color.white));
                    tvSingleEnable = false;
                    initViewEnable(MathQT.hezhiSingle());
                }else {
                    tvSingle.setBackgroundResource(R.drawable.text_pink_10_background);
                    tvSingle.setTextColor(getResources().getColor(R.color.colorTextSelect));
                    tvSingleEnable = true;
                    initViewNoEnable(MathQT.hezhiSingle());
                }
                break;
            case R.id.tv_double:
                if (tvDoubleEnable){
                    tvDouble.setBackgroundResource(R.drawable.text_red_10_background);
                    tvDouble.setTextColor(getResources().getColor(R.color.white));
                    tvDoubleEnable = false;
                    initViewEnable(MathQT.hezhiDouble());
                }else {
                    tvDouble.setBackgroundResource(R.drawable.text_pink_10_background);
                    tvDouble.setTextColor(getResources().getColor(R.color.colorTextSelect));
                    tvDoubleEnable = true;
                    initViewNoEnable(MathQT.hezhiDouble());
                }
                break;
            case R.id.ll_times:
                break;
            case R.id.tv_choose_one:
                break;
            case R.id.tv_choose_five:
                break;
            case R.id.tv_choose_ten:
                break;
            case R.id.tv_next:
                initNext();
                break;
            case R.id.tv_return:
                break;
        }
    }

    private void initViewNoEnable(ArrayList<String> arrayList) {
        for (int i1 = 0; i1 < hezhi.size(); i1++) {

            HezhiBeanNew hezhiBeanNew = hezhi.get(i1);
            //hezhiBeanNew.selectEnable = true;
            for (int i = 0; i < arrayList.size(); i++) {

                String text = hezhiBeanNew.getHezhi();

                String s = arrayList.get(i);
                if (text.equals(s)) {
                    hezhiBeanNew.selectEnable = true;
                }
            }
        }
        redAdapter.setNewData(hezhi);
    }

    private void initViewEnable(ArrayList<String> arrayList) {
        for (int i1 = 0; i1 < hezhi.size(); i1++) {

            HezhiBeanNew hezhiBeanNew = hezhi.get(i1);
            //hezhiBeanNew.selectEnable = true;
            for (int i = 0; i < arrayList.size(); i++) {

                String text = hezhiBeanNew.getHezhi();

                String s = arrayList.get(i);
                if (text.equals(s)) {
                    hezhiBeanNew.selectEnable = false;
                }
            }
        }
        redAdapter.setNewData(hezhi);
    }

    private void initNext() {
        HezhiBean bean = new HezhiBean(2,10,10,"和值",hezhi);
        ArrayList<HezhiBeanNew> list2 = bean.getmList();
        ArrayList<String> mredList = new ArrayList<>();

        for (HezhiBeanNew hezhiBeanNew : list2) {
            if (hezhiBeanNew.isSelectEnable()) {

            } else {
                mredList.add(hezhiBeanNew.getHezhi());
            }
        }
        if (mredList.size()< 1){
            initDalog();
        }else {
            EventBus.getDefault().postSticky(new HezhiBean(2,10,10,"和值",hezhi));
            startActivity(new Intent(getActivity(), ShoppingActivity.class));
            getActivity().finish();
        }
    }
    private void initDalog() {

        final Dialog dialog=new Dialog(getActivity());
        dialog.setContentView(R.layout.qthree_hezhi_prompting);
        dialog.show();
        dialog.findViewById(R.id.tv_true).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

    }
    class HezhiAdapter extends BaseQuickAdapter<HezhiBeanNew, BaseViewHolder> {

        public HezhiAdapter(List<HezhiBeanNew> data) {
            super(R.layout.layout_hezhi, data);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final HezhiBeanNew item) {
            final LinearLayout linearLayout = helper.getView(R.id.ll_hezhi);
            final TextView tvHezhi = helper.getView(R.id.tv_hezhi);
            final TextView tvJiangjin = helper.getView(R.id.tv_jiangjin);
            helper.setText(R.id.tv_hezhi, item.getHezhi());
            helper.setText(R.id.tv_jiangjin, item.getJiangjin());
            if (item.selectEnable) {
                linearLayout.setBackgroundResource(R.drawable.text_pink_10_background);
                tvHezhi.setTextColor(getResources().getColor(R.color.colorTextSelect));
                tvJiangjin.setTextColor(getResources().getColor(R.color.colorTextSelect));
            } else {
                linearLayout.setBackgroundResource(R.drawable.text_red_10_background);
                tvHezhi.setTextColor(getResources().getColor(R.color.white));
                tvJiangjin.setTextColor(getResources().getColor(R.color.white));
            }
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (item.selectEnable) {
                        linearLayout.setBackgroundResource(R.drawable.text_red_10_background);
                        tvHezhi.setTextColor(getResources().getColor(R.color.white));
                        tvJiangjin.setTextColor(getResources().getColor(R.color.white));
                        ++redcount;
                        item.selectEnable = false;
                        tvSum.setText(redcount + "");
                        tvNotesSum.setText(redcount+"");
                        tvMoneySum.setText(2*redcount+"");
                    } else {
                        linearLayout.setBackgroundResource(R.drawable.text_pink_10_background);
                        tvHezhi.setTextColor(getResources().getColor(R.color.colorTextSelect));
                        tvJiangjin.setTextColor(getResources().getColor(R.color.colorTextSelect));
                        --redcount;
                        item.selectEnable = true;
                        tvSum.setText(redcount + "");
                        tvNotesSum.setText(redcount+"");
                        tvMoneySum.setText(2*redcount+"");
                    }
                    sumHezhi = MathQT.sumHezhi(redcount);
                    sumHezhiJine = MathQT.sumHezhiJine(redcount);
                }
            });

        }
    }
}
